package com.jason.canvaslayer.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * 见图drawable: canvas_stack
 * @DESC:    canvas 内部会有一个stack栈
 *           canvas会默认保存一个底层的控件给我们绘制一些东西,当我们没有调用save方法时,
 *           所有的绘图操作都在这个Default Stack ID中进行
 *           每当我们调用一次save就会往Stack中存入一个ID,将其后所有的操作都在这个ID所指向的空间进行
 *           直到我们调用restore方法还原操作
 *
 * @Author: Jason
 * @Date: 16/9/26
 * @Time: 16:18
 */

public class CanvasSaveView4 extends View {
    private Paint mPaint;
    private int mViewWidth, mViewHeight;

    public CanvasSaveView4(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mViewWidth = w;
        mViewHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int saveID1 = canvas.save(Canvas.CLIP_SAVE_FLAG);
        canvas.clipRect(mViewWidth / 2F - 300, mViewHeight / 2F - 300,
                mViewWidth / 2F + 300, mViewHeight / 2F + 300);
        canvas.drawColor(Color.YELLOW);
        canvas.restore();

        // 保存并裁剪画布填充绿色
        int saveID2 = canvas.save(Canvas.CLIP_SAVE_FLAG);
        canvas.clipRect(mViewWidth / 2F - 200, mViewHeight / 2F - 200,
                mViewWidth / 2F + 200, mViewHeight / 2F + 200);
        canvas.drawColor(Color.GREEN);
        canvas.restore();

        // 保存画布 并旋转后绘制一个蓝色的矩形
        int saveID3 = canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.rotate(5);
        mPaint.setColor(Color.BLUE);
        canvas.drawRect(mViewWidth / 2F - 100, mViewHeight / 2F - 100,
                mViewWidth / 2F + 100, mViewHeight / 2F + 100, mPaint);
        canvas.restore();


        mPaint.setColor(Color.CYAN);
       /* canvas.drawRect(mViewWidth / 2F, mViewHeight / 2F,
                mViewWidth / 2F + 200, mViewHeight / 2F + 200, mPaint);*/
        canvas.drawRect(mViewWidth / 2F, mViewHeight / 2F,
                mViewWidth / 2F + 400, mViewHeight / 2F + 400,mPaint);

        // 总结  不restore的话  下面的canvas以之前的canvas的操作为主(旋转 大小等)


    }
}
